package com.aaa.controller;

import com.aaa.entity.User;
import com.aaa.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("user")
@Slf4j
public class UserController {

    @Resource
    UserServiceImpl userService;

    @RequestMapping("list")
    @ResponseBody
    public List<User> list(){
        return userService.list();
    }

    @RequestMapping("getId")
    @ResponseBody
    public List<User> getId(String username){
        return userService.getId(username);
    }

    //登录
    @RequestMapping("login")
    public String login(String username, String userpwd){
        List<User> login=userService.login(username, userpwd);
        if(login.size()==0){
            return "登录失败";
        }else {
            return "登录成功:"+username;
        }

    }

    //注册
    @RequestMapping("register")
    @ResponseBody
    public int register(User user){
        return userService.register(user) ;
    }


    //修改个人信息
    @RequestMapping("Upwd")
    @ResponseBody
    public int Upwd(User user){
        return userService.Upwd(user);
    }

    // 根据token判断是否已经认证，认证成功，会自动获取认证信息
    @RequestMapping("getAuthUser")
    public Authentication user(Authentication authentication){
        String name = authentication.getName();
        System.out.println("name>>>>>>"+name);

        // 存的username
        Object principal = authentication.getPrincipal();
        System.out.println(principal.toString());

        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        System.out.println(authentication.toString());

        return authentication;
    }
}
